﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Luna小店.service;
using Luna小店.UC.款式管理.service;
using Luna小店.UC.面料库存.库存记录.service;
using DevExpress.XtraGrid.Demos.util;
using Luna小店.DTO;
using mendian;
using Luna小店.UC.样品管理.样品下单新.service;
using Luna小店.UC.款式异常;
using static Luna小店.UC.门店下单.form.Frm订单一览;
using Luna小店.UC.样品管理.样品下单新;
using System.Net;
using System.IO;
using Luna小店.report;
using DevExpress.XtraReports.UI;

namespace Luna小店.UC.样品管理.样品订单预览
{
    public partial class Form样品订单预览新 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private StyleCategoryDTO styleCategoryDTO;
        private StyleAccessoryItemsDTO styleAccessoryItemsDTO;
        public DTOStyleMeasureItems dtoStyleMeasureItems { get; set; }
        private Enum订单类别 @enum;
        public List<ContrastForUCDto> contrastForUCDtos = new List<ContrastForUCDto>();
        //private Enum任务类别 enum任务类别 = Enum任务类别.销售任务;
        //public UC销售备注模板 uc销售备注模板 = new UC销售备注模板();
        //public TaskDTOS TaskDTOS = new TaskDTOS();
        //public TaskDTO TaskDTO = new TaskDTO();
        private 门店下单选款式Model model;
        private String Style_Id;
        private String ORDER_ID;
        private String REMARKS;
        private Dictionary<String, String> template_choose = new Dictionary<string, string>();//模板名称和id
        private String formId;
        public Form样品订单预览新()
        {
            InitializeComponent();
        }

        public Form样品订单预览新(String Style_Id, List<尺寸呈现dto> lst, String ORDER_ID, String REMARKS, Enum订单类别 @enum, DTOStyleMeasureItems dTO)
        {
            InitializeComponent();
            this.Style_Id = Style_Id;
            this.ORDER_ID = ORDER_ID;
            this.REMARKS = REMARKS;
            this.@enum = @enum;
            //  订单相关信息
            this.model = new 门店下单选款式Model(ORDER_ID)
                .build尺寸呈现(lst)
                .build款式全尺寸(Style_Id)
                .build设计点(Style_Id)
                .build款式图片()
                .build订单基本信息();

            //  this.model.build订单Model()
            this.dtoStyleMeasureItems = dTO;
            //  控件行为

            //this.gridControlSize.DataSource = this.model.尺寸呈现;
            //this.gridControl设计点.DataSource = this.model.Dto定制下单.Dto设计点s;
            //  模板  TODO
            //((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("样品下单");
            //((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("定制下单");
            //this.barEditItemTemplate.EditValue = ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items[0];

            this.订单基本信息呈现();
            this.styleCategoryDTO = FunctionHelper.JsonDeserialization<StyleCategoryDTO>(RestCall.httpGetMethod(URL.接口url + "/style/getCategoryByStyleId?styleId=" + this.model.Dto定制下单.Style_Id));
            this.formId = 样品下单Service.getformIdByOrderId(this.ORDER_ID);
            this.gridControl备注文件.DataSource = 样品下单Service.get备注项目详情(formId);
            this.gridView备注文件.ExpandAllGroups();



        }
        private void 订单基本信息呈现()
        {
            this.textBox销售信息姓名.Text = this.model.订单基本信息.销售信息姓名;
            this.textBox销售信息性别.Text = this.model.订单基本信息.销售信息性别;
            this.textBox销售信息电话.Text = this.model.订单基本信息.销售信息电话;
            this.textBox发运信息地址.Text = this.model.订单基本信息.收货人地址;
            this.textBox发运信息姓名.Text = this.model.订单基本信息.收货人姓名;
            this.textBox发运信息电话.Text = this.model.订单基本信息.收货人电话;

            String ref_style_id = StyleService.getStyleByORDER_ID(this.ORDER_ID).REF_STYLE_ID;
            this.textBox款名.Text = this.model.Dto定制下单.STYLE_NAME_CN;
            this.pictureBox款式.LoadAsync(款式Service.get款式图(ref_style_id));


            DTO面料库存 DTO面料库存 = 面料记录Service.get面料(this.model.面料信息[0].MATERIAL_ID);
            this.textBox面料.Text = this.model.面料信息[0].name;
            this.textBox成分.Text = this.model.面料信息[0].description;
            this.pictureBox面料.LoadAsync(面料记录Service.get面料图(DTO面料库存.materialNo));

            this.textBox尺寸.Text = this.model.订单基本信息.尺寸;

            //this.gridControl量体图片.DataSource = FileService.GetCustomerPics(this.ORDER_ID);
            //this.gridControl备注图片.DataSource = this.model.Dto定制下单.备注图片s;
        }

        private void 下载备注文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileName = FunctionHelper.GetgridViewValue(
                this.gridView备注文件,
                this.gridView备注文件.FocusedRowHandle,
                "fileName");
            string filePath = FunctionHelper.GetgridViewValue(
                this.gridView备注文件,
                this.gridView备注文件.FocusedRowHandle,
                "filePath");
            if (String.IsNullOrEmpty(fileName))
            {
                MessageBox.Show("暂无文件");
            }
            else
            {
                FolderBrowserDialog sFD = new FolderBrowserDialog();
                DialogResult rdr = sFD.ShowDialog();
                string selectFileName = sFD.SelectedPath;
                if (rdr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(selectFileName))
                {
                    try
                    {
                        //通过url下载文件
                        WebClient wClient = new WebClient();
                        wClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        wClient.DownloadFile(URL.图片url + "/sample/" + filePath, selectFileName + "\\" + fileName);
                        MessageBox.Show("保存成功");
                    }
                    catch
                    {
                        MessageBox.Show("保存失败");
                    }
                }
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Form样品文件(this.formId).ShowDialog();
            this.gridControl备注文件.DataSource = 样品下单Service.get备注项目详情(formId);
            this.gridView备注文件.ExpandAllGroups();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                new Luna小店.UC.面料库存.Frm定制订单出库申请(this.ORDER_ID, this.model.订单基本信息.ORDER_NO, this.model.MATERIAL_CODE, this.contrastForUCDtos, this.model.面料信息[0].name, this.Style_Id).ShowDialog();
            }
            catch (Exception Exception)
            {
                MessageBox.Show(Exception.Message);
            }
        }

        private void 预览备注文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileName = FunctionHelper.GetgridViewValue(
                this.gridView备注文件,
                this.gridView备注文件.FocusedRowHandle,
                "fileName");
            string filePath = FunctionHelper.GetgridViewValue(
                this.gridView备注文件,
                this.gridView备注文件.FocusedRowHandle,
                "filePath");
            //下载文件到本地项目下指定文件
            PictureService.DownloadPicture(URL.图片url + "/sample/" + filePath, @"doc\" + fileName, -1);
            string extension = Path.GetExtension(fileName);
            //文件后缀 带点（.）
            if (extension.Equals(".pdf"))
            {
                new Form备注文件预览PDF(@"doc\" + fileName).ShowDialog();
            }
            else if(extension.Equals(".doc") || extension.Equals(".docx"))
            {
                new Form备注文件预览DOC(@"doc\" + fileName).ShowDialog();
            }

        }

        private void barButtonItem订单纸预览_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.@enum == Enum订单类别.销售订单 || this.@enum == Enum订单类别.套号订单)
            {
                this.model.尺寸呈现.Clear();

                String str = FunctionHelper.JsonSerialization(dtoStyleMeasureItems.measureItemDTOS);
                foreach (MeasureItemDTO dto in dtoStyleMeasureItems.measureItemDTOS)
                {
                    this.model.尺寸呈现.Add(new 尺寸呈现dto(dto));
                }
            }
            if (this.@enum == Enum订单类别.样品订单)
            {
                this.model.尺寸呈现.Clear();
                this.model.Dto定制下单.Dto设计点s.Clear();
            }
            Image pic = pictureBox面料.Image;
            订单导出DTO 订单导出DTO = OrderService.getOrderInfo(this.ORDER_ID, this.model.尺寸呈现, this.model.Dto定制下单.Dto设计点s, pic);

            List<订单导出DTO> 订单导出DTOs = new List<订单导出DTO>();
            订单导出DTOs.Add(订单导出DTO);
            XtraReport订单 report = new XtraReport订单();
            switch (this.@enum)
            {
                case Enum订单类别.销售订单:
                    break;
                case Enum订单类别.坚山订单:
                    report.坚山颜色水印();
                    break;
                case Enum订单类别.坚山美国订单:
                    report.美国坚山颜色水印();
                    break;
                case Enum订单类别.样品订单:
                    report.样品颜色水印();
                    break;
                case Enum订单类别.套号订单:
                    report.套号颜色水印();
                    break;
            }

            report.DataSource = 订单导出DTOs;

            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
        }
    }
}